using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MasomRecieptBookApplication
{
    public partial class MainForm : Form
    {
        private DataSet ds = null;
        public MainForm(ref DataSet dataSet)
        {
            ds = dataSet;
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Declare dataBase variables
            //string cnStr, cmdText;
            //cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
            //        "Data Source= Northwind.mdb";
            //OleDbConnection cn = new OleDbConnection(cnStr);
            //OleDbCommand cmd;
           // OleDbDataReader dr;
            //DataTable dt = new DataTable("Masom Reciepts");
            try
            {
                //cn.Open();

                // Load Data into gridInfo
                //cmdText = "SELECT * FROM Orders";
                //cmd = new OleDbCommand(cmdText, cn);
                //dr = cmd.ExecuteReader();
                //if (dr.HasRows) dt.Load(dr);
                //dr.Close();

                //dg.DataSource = dt;
                dg.DataSource = ds.Tables[0].DefaultView;

                // Define a gridInfo TableStyle and add it to gridInfo
                DataGridTableStyle ts = new DataGridTableStyle();
                //ts.MappingName = dt.TableName;
                ts.MappingName = ds.Tables[0].TableName;
                dg.TableStyles.Add(ts);
                foreach (DataGridTextBoxColumn txtcol in ts.GridColumnStyles)
                {
                    txtcol.NullText = "";
                    txtcol.ReadOnly = true;
                }
                ts.RowHeadersVisible = false;
                ts.BackColor = Color.Yellow;             // Odd rows
                ts.AlternatingBackColor = Color.Yellow;  // Even rows        		
            }
            catch (Exception ex)
            {
                if (ex.Message != "Unable to cast object of type 'System.Windows.Forms.DataGridBoolColumn' to type 'System.Windows.Forms.DataGridTextBoxColumn'.")
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            finally
            {
                //cn.Close();
                //cn = null;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            // Calling gridInfo Printing
            PrintDG.Print_DataGrid(dg);
        }

        private void dg_Navigate(object sender, NavigateEventArgs ne)
        {

        }
    }
}